﻿CREATE PROCEDURE [dbo].[yaf_topic_latest](
                @BoardID  INT,
                @NumPosts INT,
                @UserID   INT)
AS
BEGIN
	SET ROWCOUNT @NumPosts
	
	SELECT
		t.LastPosted,
		t.ForumID,
		f.Name as Forum,
		t.Topic,
		t.TopicID,
		t.LastMessageID,
		t.LastUserID,
		t.NumPosts,
		LastUserName = IsNull(t.LastUserName,(select [Name] from [dbo].[yaf_User] x where x.UserID = t.LastUserID))
	FROM 
		[dbo].[yaf_Topic] t
	INNER JOIN
		[dbo].[yaf_Forum] f ON t.ForumID = f.ForumID	
	INNER JOIN
		[dbo].[yaf_Category] c ON c.CategoryID = f.CategoryID
	JOIN
		[dbo].[yaf_vaccess] v ON v.ForumID=f.ForumID
	WHERE
		c.BoardID = @BoardID
		AND t.TopicMovedID is NULL
		AND v.UserID=@UserID
		AND (v.ReadAccess <> 0)
		AND (t.Flags & 8) = 0
	ORDER BY
		t.LastPosted DESC;
END


